var express = require('express')
var cors = require('cors')
const crypto = require('crypto')

var app = express()
var md5 = require('./lib/md5')
var corsOptions = {
  origin: 'http://example.com',
  optionsSuccessStatus: 200 // some legacy browsers (IE11, various SmartTVs) choke on 204 
}

app.get('/', cors(corsOptions), function (req, res, next) {
  let cipher = md5.cipher(req.query.md5)
  var crypto = require('crypto');
  var ciphera = crypto.createCipher('aes-256-cbc', 'InmbuvP6Z8')
  var text = req.query.md5;
  var crypted = ciphera.update(text, 'utf8', 'hex')
  crypted += ciphera.final('hex')
  console.log(crypted)
  var decipher = crypto.createDecipher('aes-256-cbc', 'InmbuvP6Z8')
  var dec = decipher.update(crypted, 'hex', 'utf8')
  dec += decipher.final('utf8')
  console.log(dec)
  res.json({
    msg: 'This is CORS-enabled for only example.com.',
    cipher: cipher,
    // decipher :md5.decipher(cipher)
  })
})

app.listen(800, function () {
  console.log('CORS-enabled web server listening on port 800')
})
